// pages/user/combo/query/query.js
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
Page({
  /**
   * 页面的初始数据
   */
  data: {
    key: '',
    index: '',
    tabs: [{
        key: 'tab1',
        title: '购买记录',
      },
      {
        key: 'tab2',
        title: '使用记录',
      },
      {
        key: 'tab3',
        title: '已生成合同',
      }
    ],
    buyIndex: -1,
    pageSize: 10,
    buiedRecordList: [],
    isLoadedBuied: false,
    isFirstTipBuied: true,
    pageBuied: 1,

    useRecordList: [],
    isLoadedUse: false,
    isFirstTipUse: true,
    pageUse: 1,


    madedCompList: [],
    isLoadedPact: false,
    isFirstTipPact: true,
    pagePact: 1,
    packSelectIndex: -1,
    packDefaultImg: '/resource/image/radio_normal.png',
    packSelectImg: '/resource/image/radio_select.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let platform = getApp().globalData.platform;
    if (platform == 'ios') {
      this.setData({
        platform
      })
    }
  },  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    token = getApp().globalData.token;
    this.getBuyList();
    this.getUseList();
    this.getPactList();
  },

  onTabsChange(e) {
    const {
      key
    } = e.detail;
    const index = this.data.tabs.map((n) => n.key).indexOf(key);

    this.setData({
      key,
      index,
    });
    if (index == 2) {
      this.setData({
        madedCompList: [],
        isLoadedPact: false,
        isFirstTipPact: true,
        pagePact: 1,
      })
      this.getPactList();
    }
  },

  onSwiperChange(e) {
    const {
      current: index,
      source
    } = e.detail
    const {
      key
    } = this.data.tabs[index]

    if (!!source) {
      this.setData({
        key,
        index,
      });
      if (index == 2) {
        this.setData({
          madedCompList: [],
          isLoadedPact: false,
          isFirstTipPact: true,
          pagePact: 1,
        })
        this.getPactList();
      }
    }
  },

  getBuyList() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'order/buy_list',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pageBuied,
        size: this.data.pageSize
      },
      success: (res) => {
        if (res.rowCount = this.data.buiedRecordList.length) {
          this.data.isLoadedBuied = true;
          return
        }
        this.setData({
          buiedRecordList: this.data.buiedRecordList.concat(res.list)
        })
      }
    })
  },

  getUseList() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'order/expense_list',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pageUse,
        size: this.data.pageSize
      },
      success: (res) => {
        if (res.rowCount = this.data.useRecordList.length) {
          this.data.isLoadedUse = true;
          return
        }
        this.setData({
          useRecordList: this.data.useRecordList.concat(res.list)
        })
      }
    })
  },
  getPactList() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'order/contract_list',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pagePact,
        size: this.data.pageSize
      },
      success: (res) => {
        if (res.rowCount = this.data.madedCompList.length) {
          this.data.isLoadedPact = true;
          return
        }
        this.setData({
          madedCompList: this.data.madedCompList.concat(res.list)
        })
      }
    })
  },

  onLoadMoreBuyList() {
    if (this.data.isLoadedBuied) {
      if (this.data.isFirstTipBuied) {
        T.toast({ id: '#toast', text: '已加载完全部数据了' });
      }
      this.data.isFirstTipBuied = false;
      return;
    }
    this.data.pageBuied += 1;
    this.getBuyList();
  },
  onLoadMoreUseList() {
    if (this.data.isLoadedUse) {
      if (this.data.isFirstTipUse) {
        T.toast({ id: '#toast', text: '已加载完全部数据了' });
      }
      this.data.isFirstTipUse = false;
      return;
    }
    this.data.pageUse += 1;
    this.getUseList();
  },
  onLoadMorePact() {
    if (this.data.isLoadedPact) {
      if (this.data.isFirstTipPact) {
        T.toast({ id: '#toast', text: '已加载完全部数据了' });
      }
      this.data.isFirstTipPact = false;
      return;
    }
    this.data.pagePact += 1;
    this.getPactList();
  },

  onSelectPack(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    this.setData({
      downPackId: id,
      packSelectIndex: index
    })
    console.log('packSelectIndex:' + this.data.packSelectIndex)
  },

  onSelectBuiedRecord(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      buyIndex: index
    })
  },

  /**
   * 生成合同
   */
  onMadeCompound(e) {
    let index = this.data.buyIndex;
    if (index == -1) {
      T.toast({ id: '#toast', text: '请选择记录' });
      return ;
    }
    let param = {
      token: token,
      id: this.data.buiedRecordList[index].id
    }
    T.req({
      loadTitle: '请求中...',
      url: config.api + 'order/creates_contract?' + T.param(param),
      method: 'POST',
      success: (res) => {
        T.toast({ id: '#toast', text: '生成成功', success: () => {
          this.data.buyIndex = -1;
          this.data.buiedRecordList[index].createContract = 1;
          this.setData({
            buiedRecordList: this.data.buiedRecordList
          })
        }});
      }
    })
  },

  onDownPack() {
    let id = this.data.downPackId;
    if (this.data.packSelectIndex == -1) return ;
    let param  = {
      token: token,
      id: id
    }
    wx.downloadFile({
      url: config.api + 'order/download_contract?' + T.param(param),
      success: (res) => {
        wx.openDocument({
          filePath: res.tempFilePath,
          success: (res) => {
            this.data.packSelectIndex = -1;
            this.data.downPackId = -1;
            console.log(res)
          },
          fail: (err) => {
            console.log(err)
          }
        })
      }
    })
  }

})